package com.distribute.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codingapi.tx.annotation.TxTransaction;
import com.distribute.common.Constant;
import com.distribute.dao.TOrderItemMapper;
import com.distribute.dao.TOrderMapper;
import com.distribute.dao.TSysUserMapper;
import com.distribute.dto.LoginUser;
import com.distribute.entity.TOrder;
import com.distribute.entity.TOrderItem;
import com.distribute.entity.TSysUser;
import com.distribute.entity.TSysUserExample;
import com.distribute.service.AdminService;
import com.distribute.service.UserService;

import io.shardingsphere.core.keygen.DefaultKeyGenerator;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private TSysUserMapper userDao;
	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Resource
    private TOrderMapper orderRepository;
    
    @Resource
    private TOrderItemMapper orderItemRepository;
	@Autowired
	private AdminService adminService;
    
	@Override
	@Transactional
	public TSysUser save(TSysUser user) {
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		user.setCreateUserId(0L);
		user.setCreateUserName("");
		user.setUpdateUserId(0L);
		user.setUpdateUserName("");
		user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
		userDao.insertSelective(user);
		return user;
	}
	// unusered
	@Override
	public TSysUser getUser(HttpSession session) {
		LoginUser loginUser = (LoginUser) session.getAttribute(Constant.SESSION_USER);
		return loginUser;
	}

	// 分表测试
	@Override
	public List<TOrderItem> shardingJdbc() {

		orderRepository.createIfNotExistsTable();
		orderItemRepository.createIfNotExistsTable();
		orderRepository.truncateTable();
		orderItemRepository.truncateTable();
		List<Long> orderIds = new ArrayList<>(10);
		System.out.println("1.Insert--------------");
		for (int i = 0; i < 10; i++) {
			// Integer orderId = 40 + i;
			Long orderId = getUniqueId2();
			TOrder order = new TOrder();
			order.setOrderId(orderId); // 根据orderId分表
			order.setUserId(i);
			order.setStatus("INSERT_TEST  根据orderId分表");
			orderRepository.insert2(order);

			TOrderItem item = new TOrderItem();
			item.setOrderId(orderId); // 根据orderId分表
			item.setUserId(i);
			orderItemRepository.insert2(item);
		}
		System.out.println("------分表与分表join 查询--------");
		System.out.println(orderItemRepository.selectAll());
		return orderItemRepository.selectAll();
		// System.out.println(orderItemRepository.selectUserList() +
		// "---------分表与不分表join 查询-------------");
		/*
		 * System.out.println("2.Delete--------------"); for (Long each :
		 * orderIds) { orderRepository.delete(each);
		 * orderItemRepository.delete(each); }
		 * System.out.println(orderItemRepository.selectAll());
		 * orderItemRepository.dropTable(); orderRepository.dropTable();
		 */
	}
	
	// 获取分布式主键
    public Long getUniqueId2() {
    	// 默认分布式生成主键
    	DefaultKeyGenerator keyGenerator =  new DefaultKeyGenerator();
    	Number key = keyGenerator.generateKey();
    	
    	return System.currentTimeMillis();
    }

	
    // 分布式事务测试
    @Override
    @TxTransaction(isStart = true)
    @Transactional
	public void testTransaction() throws Exception {
		
    	//member服务插入数据测试
    	Long orderId = getUniqueId2();
		TOrder order = new TOrder();
		order.setOrderId(orderId); // 根据orderId分表
		order.setUserId(new Random().nextInt());
		order.setStatus("member服务插入数据测试");
		orderRepository.insert2(order);
    	
    	
    	//调用admin服务插入数据测试
		order = new TOrder();
		order.setUserId(new Random().nextInt());
		order.setStatus("admin服务插入数据测试");
		adminService.insertSelective(order);
		
    	//int a = 1/0;
	}

	@Override
	public TSysUser getByName(String username) {
		TSysUserExample example = new TSysUserExample();
		example.createCriteria().andUsernameEqualTo(username);
		List<TSysUser> userList = userDao.selectByExample(example);
		return userList == null ? null : userList.get(0);
	}

}
